package com.hq.fxh.imageLoader;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.hq.fxh.R;
import com.hq.fxh.Utils.Tool;
import com.hq.fxh.imageLoader.bean.ImageFolder;

import java.util.ArrayList;
import java.util.List;


/**
 * ================================================
 * 作    者：jeasonlzy（廖子尧 Github地址：https://github.com/jeasonlzy0216
 * 版    本：1.0
 * 创建日期：2016/5/19
 * 描    述：
 * 修订历史：
 * ================================================
 */
public class ImageFolderAdapter extends RecyclerView.Adapter<ImageFolderAdapter.MyViewHolder> {

    private ImagePicker imagePicker;
    private Activity mActivity;
    private int mImageSize;
    private List<ImageFolder> imageFolders;
    private int lastSelected = 0;

    public ImageFolderAdapter(Activity activity, List<ImageFolder> folders) {
        mActivity = activity;
        if (folders != null && folders.size() > 1) {
            folders.remove(0);
            imageFolders = folders;
        }
        else imageFolders = new ArrayList<>();
        imagePicker = ImagePicker.getInstance();
        mImageSize = getImageItemWidth(mActivity);
    }

    public void refreshData(List<ImageFolder> folders) {
        if (folders != null && folders.size() > 0) imageFolders = folders;
        else imageFolders.clear();
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mActivity).inflate(R.layout.adapter_folder_image_item, parent, false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        final ImageFolder folder = imageFolders.get(position);
        holder.tag.setVisibility(View.GONE);
        holder.folderName.setText(folder.name);
        holder.imageCount.setText(mActivity.getString(R.string.folder_image_count, folder.images.size()));
        imagePicker.getImageLoader().displayImage(mActivity, folder.cover.path, holder.cover, mImageSize, mImageSize);

        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("OneimageFolder", folder);
                bundle.putBoolean("isTransFolder", true);
                bundle.putBoolean("IsAddMorePic",true);
                imagePicker.setCurrentImageFolderPosition(position+1);
                Tool.openActivity(mActivity, SelectPictureActivity.class, bundle);
            }
        });
    }

    @Override
    public int getItemCount() {
        return imageFolders != null ? imageFolders.size() : 0;
    }

    public void setSelectIndex(int i) {
        if (lastSelected == i) {
            return;
        }
        lastSelected = i;
        notifyDataSetChanged();
    }

    public int getSelectIndex() {
        return lastSelected;
    }

    /**
     * 根据屏幕宽度与密度计算GridView显示的列数， 最少为三列，并获取Item宽度
     */
    public static int getImageItemWidth(Activity activity) {
        int screenWidth = activity.getResources().getDisplayMetrics().widthPixels;
        int densityDpi = activity.getResources().getDisplayMetrics().densityDpi;
        int cols = screenWidth / densityDpi;
        cols = cols < 3 ? 3 : cols;
        int columnSpace = (int) (2 * activity.getResources().getDisplayMetrics().density);
        return (screenWidth - columnSpace * (cols - 1)) / cols;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView cover,tag;
        TextView folderName;
        TextView imageCount;
        View view;

        public MyViewHolder(View view) {
            super(view);
            this.view = view;
            cover = (ImageView) view.findViewById(R.id.iv_cover);
            folderName = (TextView) view.findViewById(R.id.tv_folder_name);
            imageCount = (TextView) view.findViewById(R.id.tv_image_count);
            tag = (ImageView)view.findViewById(R.id.iv_tag);
            view.setTag(this);
        }
    }

}
